package com.yb.xczx.cloud.search.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import org.springframework.http.HttpHeaders;

@Configuration
public class EsClientConfig extends ElasticsearchConfiguration {

	@Autowired
	ElasticsearchProperties elasticsearchProperties;

	@Override
	public ClientConfiguration clientConfiguration() {
		HttpHeaders compatibilityHeaders = new HttpHeaders();
		compatibilityHeaders.add("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
		compatibilityHeaders.add("Content-Type", "application/vnd.elasticsearch+json;"
				+ "compatible-with=7");
		return ClientConfiguration.builder()
			.connectedTo(elasticsearchProperties.getUris().toArray(new String[0]))
				.withBasicAuth(elasticsearchProperties.getUsername(), elasticsearchProperties.getPassword())
				.withDefaultHeaders(compatibilityHeaders)    // this variant for imperative code
//				.withHeaders(() -> compatibilityHeaders)
				.build();
	}
}
